﻿using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class CheckBoxReader : IValueReader
    {
        public IEnumerable<Answer> Read(Question question, IDictionary<string, string> f)
        {
            var value = f["q" + question.Id];
            if (value.Substring(0, 4).Equals("true"))
            {
                yield return new Answer { QuestionId = question.Id };

                foreach (
                    var q in question.Children.SelectMany(child => ValueReaderFactory.Get(child.Type).Read(child, f)))
                    yield return q;
            }
        }
    }
}